
#include <OneWire.h>

OneWire ds1(30);
OneWire ds2(31);
OneWire ds3(29);
OneWire ds4(33);
OneWire ds5(34);
OneWire ds6(35);
OneWire ds7(36);

//======================== получаем температуру ============================
float getTemperature (int num, boolean test){
  byte i;
  byte type_s;
  byte data[12];
  byte addr[8];
  num=num+1;

  if(num == 1){
    ds1.reset_search();
    if  (!ds1.search(addr)) {
      if(test) return 0;
      ds1.reset_search();
      return getTemperature (num, test);
    }
    ds1.reset ();
    ds1.select (addr);
    ds1.write (0x44,1);
    ds1.reset ();
    ds1.select (addr); 
    ds1.write (0xBE); // Read Scratchpad 1
    for  (i = 0; i < 9; i++) data[i] = ds1.read  ();
  } else if(num == 2){
    ds2.reset_search();
    if  (!ds2.search(addr)) {
      if(test) return 0;
      ds2.reset_search();
      return getTemperature (num, test);
    }
    ds2.reset ();
    ds2.select (addr);
    ds2.write (0x44,1);
    ds2.reset ();
    ds2.select (addr); 
    ds2.write (0xBE); // Read Scratchpad 2
    for  (i = 0; i < 9; i++) data[i] = ds2.read  ();
  } else if(num == 3){
    ds3.reset_search();
    if  (!ds3.search(addr)) {
      if(test) return 0;
      ds3.reset_search();
      return getTemperature (num, test);
    }
    ds3.reset ();
    ds3.select (addr);
    ds3.write (0x44,1);
    ds3.reset ();
    ds3.select (addr); 
    ds3.write (0xBE); // Read Scratchpad 3
    for  (i = 0; i < 9; i++) data[i] = ds3.read  ();
  } else if(num == 4){
    ds4.reset_search();
    if  (!ds4.search(addr)) {
      if(test) return 0;
      ds4.reset_search();
      return getTemperature (num, test);
    }
    ds4.reset ();
    ds4.select (addr);
    ds4.write (0x44,1);
    ds4.reset ();
    ds4.select (addr); 
    ds4.write (0xBE); // Read Scratchpad 4
    for  (i = 0; i < 9; i++) data[i] = ds4.read  ();
  } else if(num == 5){
    ds5.reset_search();
    if  (!ds5.search(addr)) {
      if(test) return 0;
      ds5.reset_search();
      return getTemperature (num, test);
    }
    ds5.reset ();
    ds5.select (addr);
    ds5.write (0x44,1);
    ds5.reset ();
    ds5.select (addr); 
    ds5.write (0xBE); // Read Scratchpad 5
    for  (i = 0; i < 9; i++) data[i] = ds5.read  ();
  } else if(num == 6){
    ds6.reset_search();
    if  (!ds6.search(addr)) {
      if(test) return 0;
      ds6.reset_search();
      return getTemperature (num, test);
    }
    ds6.reset ();
    ds6.select (addr);
    ds6.write (0x44,1);
    ds6.reset ();
    ds6.select (addr); 
    ds6.write (0xBE); // Read Scratchpad 6
    for  (i = 0; i < 9; i++) data[i] = ds6.read  ();
  } else if(num == 7){
    ds7.reset_search();
    if  (!ds7.search(addr)) {
      if(test) return 0;
      ds7.reset_search();
      return getTemperature (num, test);
    }
    ds7.reset ();
    ds7.select (addr);
    ds7.write (0x44,1);
    ds7.reset ();
    ds7.select (addr); 
    ds7.write (0xBE); // Read Scratchpad 7
    for  (i = 0; i < 9; i++) data[i] = ds7.read  ();
  } else return 0;


  if(test) return 1;
  // convert the data to actual temperature

  unsigned int raw =  (data[1] << 8) | data[0];
  if  (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if  (data[7] == 0x10) {
    // count remain gives full 12 bit resolution
      raw =  (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg =  (data[4] & 0x60);
    if  (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
    else if  (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
    else if  (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
    // default is 12 bit resolution, 750 ms conversion time
  }
  return (float)raw / 16.0;
} 

//======================== устанавливаем действие ============================
void setAction(int num){
  float current = elements[num][4]/100.00; //текущая температура
  float last = elements[num][5]/100.00;    //последняя температура
  float needed = elements[num][3]/100.00;  //необходимая температура
  float limit = elements[num][6]/100.00;  //температура допустимого отклонения
  int currentAction = elements[num][2];  //текущее действие
  if(current>needed+limit) action(num,1);
  else if (current<needed-limit) action(num,-1);
  else if((currentAction>0 && current<needed) || (currentAction<0 && current>needed)) action(num,0);
}

//======================== посылаем действие ============================
void action(int num, int delta){
  elements[num][2] = delta;
  if(delta>0) { //перегрев
    Serial.print(elements[num][7]);
    Serial.print(" ");
    Serial.println(1);
    Serial.print(elements[num][8]);
    Serial.print(" ");
    Serial.println(0);
    digitalWrite(elements[num][7],HIGH);
    digitalWrite(elements[num][8],LOW);
  }
  else if (delta<0) { //переохлаждение
    Serial.print(elements[num][7]);
    Serial.print(" ");
    Serial.println(0);
    Serial.print(elements[num][8]);
    Serial.print(" ");
    Serial.println(1);
    digitalWrite(elements[num][7],LOW);
    digitalWrite(elements[num][8],HIGH);
  }else{ //норма
    Serial.print(elements[num][7]);
    Serial.print(" ");
    Serial.println(0);
    Serial.print(elements[num][8]);
    Serial.print(" ");
    Serial.println(0);
    digitalWrite(elements[num][7],LOW);
    digitalWrite(elements[num][8],LOW);
  }
}

